package com.kotlin.myapplicationd.base;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.kotlin.myapplicationd.model.CommonPresenter;
import com.trello.rxlifecycle.components.support.RxFragment;

import java.util.HashMap;
import java.util.Map;

/**
 * Fragment基类(V4包)
 *
 * @author
 * @version 1.0
 */
public abstract class BaseFragmentV4 extends RxFragment implements IBaseFragment {

    /**
     * 日志输出标志
     **/
    protected final String TAG = this.getClass().getSimpleName();
    public boolean isPrepared = false;
    /**
     * 共通操作
     **/
    protected BaseOperation mOperation = null;
    protected CommonPresenter mCommonPresenter;//共用的
    /**
     * 依附的Activity
     **/
    protected Activity mContext = null;
    protected boolean isVisible;//Fragment is Visible
    // -----------------------------------
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    /**
     * 当前Fragment渲染的视图View
     **/
    private View mContextView = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        // 缓存当前依附的activity
        mContext = activity;
        Log.d(TAG, "BaseFragmentV4-->onAttach()");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "BaseFragmentV4-->onCreate()");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(TAG, "BaseFragmentV4-->onCreateView()");
        //ToolAlert.toastLong("business_workbench");

        // 渲染视图View
        if (null == mContextView) {
            // 初始化参数
            initParms(getArguments());

            //View mView = bindView();
            //if (null == mView) {
            mContextView = inflater.inflate(bindLayout(), container, false);
            //} else {
            //	mContextView = mView;
            //}
            // 控件初始化
            initView(mContextView);
            // 实例化共通操作
            mOperation = new BaseOperation(getActivity());
            mCommonPresenter = new CommonPresenter(getActivity());
            // 业务处理

            doBusiness(getActivity());

            initListener();

            isPrepared = true;

            //lazyLoad();
        }

        return mContextView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.d(TAG, "BaseFragmentV4-->onActivityCreated()");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.d(TAG, "BaseFragmentV4-->onStart()");
        super.onStart();
    }

    @Override
    public void onResume() {
        Log.d(TAG, "BaseFragmentV4-->onResume()");
        super.onResume();

    }

    @Override
    public void onPause() {
        Log.d(TAG, "BaseFragmentV4-->onPause()");
        super.onPause();

    }

    @Override
    public void onStop() {
        Log.d(TAG, "BaseFragmentV4-->onStop()");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mContextView != null && mContextView.getParent() != null) {
            ((ViewGroup) mContextView.getParent()).removeView(mContextView);

        }
    }

    @Override
    public void onDestroy() {
        Log.d(TAG, "BaseFragmentV4-->onDestroy()");

        super.onDestroy();
        destroy();
    }

    @Override
    public void onDetach() {
        Log.d(TAG, "BaseFragmentV4-->onDetach()");
        super.onDetach();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        Log.d(TAG, "BaseFragmentV4-->onSaveInstanceState()");
        super.onSaveInstanceState(outState);
    }

    /**
     * 获取当前Fragment依附在的Activity
     *
     * @return
     */
    public Activity getContext() {
        return getActivity();

    }

    /**
     * 获取共通操作机能
     */
    public BaseOperation getOperation() {
        return this.mOperation;
    }

    public CommonPresenter getGlobalCommonPresenter() {
        return this.mCommonPresenter;
    }

    public Map<String, Object> getParamsMap() {
        return this.paramsMap;
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }

    }

    protected void onVisible() {
        if (isPrepared && isVisible) {
            lazyLoad();
        }
    }

    protected void onInvisible() {
    }

    protected abstract void lazyLoad();


}
